import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';
import 'package:wechat_assets_picker_plus/wechat_assets_picker_plus.dart';

class ReportEditItem extends StatelessWidget {
  // cell类型
  final ReportCellType? cellType;

  // 输入框类型
  final InputType? inputType;

  // title
  final String? title;

  // title样式
  final TextStyle? titleStyle;

  // hintText
  final String? hintText;

  // hintText样式
  final TextStyle? hintTextStyle;

  // counterText样式
  final TextStyle? counterTextStyle;

  // value
  final RxString value;

  // value样式
  final TextStyle? valueStyle;

  // textFeild控制器
  final TextEditingController? textEditingController;

  // 回调TextFeild的值
  final Function(String)? callbackTextFeildValue;

  // 点击事件
  final Function(ItemOnTapType, AssetModel?)? onTap;

  // 必要的
  final bool? isVital;

  // 背景 height
  final double? bgHeight;

  // title - value space
  final double? space;

  // maxLength
  final int? maxLength;

  // 资源
  final RxList<AssetModel>? assetModels;

  const ReportEditItem(
    this.cellType,
    this.inputType,
    this.title,
    this.hintText,
    this.value, {
    super.key,
    this.titleStyle,
    this.hintTextStyle,
    this.valueStyle,
    this.counterTextStyle,
    this.textEditingController,
    this.callbackTextFeildValue,
    this.onTap,
    this.isVital,
    this.bgHeight = 48,
    this.maxLength = 50,
    this.space = 8,
    this.assetModels,
  });

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      child: Container(
        padding: EdgeInsets.all(CONTENT_MARGIN),
        color: Colors.black,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Row(
              children: [
                CommonWidgets.titleText(title ?? ''),
                Visibility(
                    visible: isVital ?? true, child: CommonWidgets.markText())
              ],
            ),
            SizedBox(height: space),
            InkWell(
              onTap: () {
                if (onTap != null) {
                  onTap!(ItemOnTapType.nothing, null);
                }
              },
              child: ClipRRect(
                borderRadius: BorderRadius.circular(4),
                child: Container(
                  width: double.infinity,
                  color: cellType == ReportCellType.assets
                      ? Colors.black
                      : const Color(0xFF141414),
                  child: Padding(
                    padding: EdgeInsets.only(
                      left: cellType == ReportCellType.assets || cellType == ReportCellType.textView || cellType == ReportCellType.textFeild
                          ? 0
                          : CONTENT_MARGIN,
                    ),
                    child: _content(),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _content() {
    switch (cellType) {
      case ReportCellType.arrow:
        return Obx(
          () => Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              value.isNotEmpty
                  ? CommonWidgets.valueText(value.value.replaceAll(RegExp(r'\x00'), ''))
                  : CommonWidgets.placeholderText(hintText ?? '1'),
              CommonWidgets.listArrow(),
            ],
          ),
        );
      case ReportCellType.text:
      case ReportCellType.textView:
      case ReportCellType.textFeild:
        return Obx(
          () => LxTextField(
            value: value.value,
            placeholder: hintText,
            textFieldType: inputType,
            controller: textEditingController ??
                TextEditingController(
                  text: value.value,
                ),
            bgMargin: EdgeInsetsDirectional.only(start: 0, end: CONTENT_MARGIN),
            bgPadding: EdgeInsets.only(
              left: 0,
              top: cellType == ReportCellType.text ? 0 : 10,
              bottom: cellType == ReportCellType.text ? 0 : 10,
            ),
            bgHeight: bgHeight,
            bgBorder: const Border(
                bottom: BorderSide(width: 1.0, color: Colors.transparent)),
            maxLines: cellType == ReportCellType.textView ? 10 : 1,
            maxLength: maxLength,
            showClear: cellType == ReportCellType.text ? true : false,
            textAlign: TextAlign.left,
            onChangedValue: (value) {
              print('change: $value');
              if (callbackTextFeildValue != null) {
                callbackTextFeildValue!(value);
              }
            },
          ),
        );

      case ReportCellType.assets:
        return Obx(
          () => MediaContentEditView(
            width: Get.width - 2 * CONTENT_MARGIN,
            assetModels: assetModels!.value,
            isSupportDelete: true,
            addOnTap: () {
              if (onTap != null) {
                onTap!(ItemOnTapType.addMedia, null);
              }
            },
            deleteOnTap: (model) {
              if (onTap != null) {
                onTap!(ItemOnTapType.deleteMedia, model);
              }
            },
            previewOnTap: (model) {
              if (onTap != null) {
                onTap!(ItemOnTapType.previewMedia, model);
              }
            },
          ),
        );
      default:
        return Container();
    }
  }
}
